package com.lry.basic.thread;



/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2020/1/6
 */
//一个线程打印1 2 3 ...
//一个线程打印a b c ...
//交替打印 1 a 2 b 3 c  ... 直到所有字母打印完毕
public class Thread1 {
    private static int index = 1;
    private  static Object o = new Object();

    public static void main(String[] args) {
        new Thread(){
            @Override
            public void run() {
                synchronized (o){
                    while(index<=26){
                        System.out.println(index++);
                        //唤醒B线程
                        o.notifyAll();

                        if(index==27){
                            //最后一次不能再等待，否则会死等
                            break;
                        }

                        //阻塞自己
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                synchronized (o) {
                    while(index<=27){
                        char c = (char) ('a' + (index - 2));
                        System.out.println(c);

                        if(index==27){
                            //最后一次不能再等待，否则会一直死等
                            break;
                        }
                        //唤醒A线程
                        o.notifyAll();

                        //阻塞自己
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }
            }
        }.start();
    }


}
